package com.jiazhong.servlet.service.impl;

import com.jiazhong.commons.bean.S10User;
import com.jiazhong.servlet.mapper.S10UserMapper;
import com.jiazhong.servlet.service.S10UserService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.http.HttpSession;
import java.io.InputStream;

public class S10UserServiceImpl implements S10UserService {

    private S10UserMapper mapper;
    private SqlSessionFactory sqlSessionFactory;

    public S10UserServiceImpl() {
        InputStream inputStream = S10UserServiceImpl.class.getClassLoader().getResourceAsStream("config.xml");
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Override
    public JsonResult login(String username, String password, HttpSession session) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        mapper = sqlSession.getMapper(S10UserMapper.class);
        S10User user = mapper.login(username, password);
        if (user == null) {
            return new JsonResult(500, "账号或者密码错误");
        }
        session.setAttribute("user", user);
        return new JsonResult(user);
    }
}
